package com.cj.user.demo;

import java.util.*;
import java.util.stream.Collectors;

public class Demo1 {
    public String name;
    public int age;
    public Demo1() {
    }
    public Demo1(String name, int age) {
        this.age = age;
        this.name = name;
    }

    @Override
    public String toString() {
        return "Demo1{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    public int test1() {
//        int a = 1;
//        int b = 2;
//        try {
//            return a;
//        } finally {
//            return b;
//        }

        try {
            System.out.println("Try to do something");
            throw new RuntimeException("RuntimeException");
        } catch (Exception e) {
            System.out.println("Catch Exception -> " + e.getMessage());
            // 终止当前正在运行的Java虚拟机
            System.exit(1);
        } finally {
            System.out.println("Finally");
        }

        return 1;
    }

    public void test2() throws ClassNotFoundException {
        Class appTestClass = Demo1.class;
        System.out.println("第一种:" + appTestClass.getDeclaredMethods()[1]);
        Class<?> aClass = Class.forName("com.cj.user.demo.Demo1");
        System.out.println("第二种:" + aClass.getDeclaredMethods()[1]);
        Demo1 demo1 = new Demo1("张三", 12);
        Class<? extends Demo1> aClass1 = demo1.getClass();
        System.out.println("第三种:" + aClass1.getDeclaredMethods()[1]);
        Class<?> aClass2 = ClassLoader.getSystemClassLoader().loadClass("com.cj.user.demo.Demo1");
        System.out.println("第四种:" + aClass2.getDeclaredMethods()[1]);
    }

    public void test3() {
        double a = 1.0;
        double b = 1.00;
        int c = 1;
        System.out.println(Objects.equals(b, c));
    }

    public void test4() {
        List<String> list = new ArrayList<>();
        List<String> list2 = new Vector<>();
        list.add("1212");
        list.add("1212");
        System.out.println(list);
        Set set = new HashSet();
        set.add("123");
        set.add("123");
        System.out.println(set);
        List list1 = new LinkedList();
        list1.add("123");
        list1.add("123");
        System.out.println(list1);
    }

    public void test5() {
        Map<String,Object> map = new HashMap<>();
        map.put(null, "12");
        map.put("a", "123");
        map.put("b", "1234");
        for (Object key : map.keySet()) {
            System.out.println(key + ":" + map.get(key));
        }

        map.forEach((key, value) -> {
            System.out.println(key + ":" + value);
        });

        map.entrySet().stream().forEach(item ->{
            System.out.println(item.getKey() + ":" + item.getValue());
        });
    }

    public void test6() {
        List<Integer> list = new ArrayList<>();
        list.removeIf(filter -> filter % 2 == 0);

        Map<String, Object> map = new HashMap<>();
        map.put("a", 1);
        map.put("b", 2);
        System.out.println(map.size());
        System.out.println(map.isEmpty());
    }

    public void test7() {
//        List<Demo1> list = new ArrayList<>();
//        list.add(new Demo1("张三", 12));
//        list.add(new Demo1("张三", 13));
//        list.add(new Demo1("张三", 12));
//        System.out.println(list);
//        Set set = new HashSet(list);
//        System.out.println(set);
//        list.toArray();

        Integer[] myArray = {1, 2, 3};
        List myList = Arrays.asList(myArray);
        System.out.println(myList.get(1));//1
        System.out.println(myArray.getClass());
        System.out.println(new ArrayList<>().getClass());

        List myList1 = new ArrayList(Arrays.asList(myArray));
        System.out.println(myList1.get(2));

        List collect = Arrays.stream(myArray).collect(Collectors.toList());
    }

    public void test8() {
        Stack stack = new Stack();
        stack.push("aa");
        stack.push("bb");
        System.out.println(stack.pop());

        Queue queue = new ArrayDeque();
        queue.add("aa");
        queue.add("bb");
        System.out.println(queue.poll());
    }


    public static void main(String[] args) throws ClassNotFoundException {
        Demo1 demo1 = new Demo1();
        demo1.test8();
    }
}
